package com.gmu.service.impl;

import com.gmu.domain.ResResult;
import com.gmu.mapper.TblOrderMapper;
import com.gmu.mapper.TblSampleMapper;
import com.gmu.mapper.TblUserMapper;
import com.gmu.pojo.TblOrder;
import com.gmu.pojo.TblSample;
import com.gmu.pojo.TblUser;
import com.gmu.pojo.TblVolunteer;
import com.gmu.mapper.TblVolunteerMapper;
import com.gmu.service.TblVolunteerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 志愿者申请 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Service
public class TblVolunteerServiceImpl extends ServiceImpl<TblVolunteerMapper, TblVolunteer> implements TblVolunteerService {

    @Autowired
    private TblVolunteerMapper tblVolunteerMapper;

    @Autowired
    private TblUserMapper tblUserMapper;

    @Autowired
    private TblOrderMapper tblOrderMapper;

    @Autowired
    private TblSampleMapper tblSampleMapper;

    @Override
    public ResResult<?> volunteerOrder(Integer userId, Integer activitiesAddressId) {
        TblVolunteer tblVolunteer = new TblVolunteer();
        tblVolunteer.setUserId(userId);
        tblVolunteer.setActivitiesAddressId(activitiesAddressId);
        tblVolunteer.setApplyTime(new Date());
        tblVolunteer.setStatus(2);//申请待定中
        if ((tblVolunteerMapper.insert(tblVolunteer)) < 1) {
            return new ResResult<>(500,"申请失败");
        }
        return new ResResult<>(200,"申请成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResResult<?> volunteerWirthUserInfo(String idNumber, Integer activitiesId) {
        TblSample tblSample_ = tblSampleMapper.getSampleByIdNumberActivitiesId(idNumber, activitiesId);
        if (Objects.isNull(tblSample_)){
            return insertTblSample(idNumber,activitiesId);
        }else {
            return commitTestTime(idNumber,activitiesId);
        }
    }

    public ResResult<?> insertTblSample(String idNumber, Integer activitiesId){
        if ((tblSampleMapper.selectCountSample(idNumber,activitiesId)) > 0){
            return new ResResult<>(500,"不可重复录入");
        }
        TblUser tblUser = tblUserMapper.getUserByIdNumber(idNumber);
        if (Objects.isNull(tblUser)){
            return new ResResult<>(404,"身份证号错误");
        }
        TblOrder tblOrder = tblOrderMapper.searchOrderByUserIdAndActivitiesId(tblUser.getUserId(), activitiesId, idNumber);
        if (Objects.isNull(tblOrder)){
            return new ResResult<>(404,"您并未预约");
        }
        TblSample tblSample = new TblSample();
        tblSample.setUnitsId(tblUser.getUnitsId());
        tblSample.setUserId(tblUser.getUserId());
        tblSample.setIdNumber(idNumber);
        tblSample.setActivitiesId(activitiesId);
        tblSample.setSampleTime(new Date());
        tblSample.setStatus(0);//已录入
        if ((tblSampleMapper.insert(tblSample)) < 1){
            return new ResResult<>(500,"录入失败");
        }
        return new ResResult<>(200,"录入成功");
    }

    public ResResult<?> commitTestTime(String idNumber, Integer activitiesId){
        Integer count = tblSampleMapper.getSampleByIdNumberAndActivitiesId(idNumber,activitiesId,1);//已进行第二次扫码
        if (count > 0){
            return new ResResult<>(500,"不可重复提交");
        }
        if ((tblOrderMapper.setOrderToTested(idNumber,activitiesId)) < 1){
            return new ResResult<>(500,"您并未预约");
        }
        if ((tblSampleMapper.commitTestTime(idNumber,activitiesId,new Date(),1) < 1)){
            return new ResResult<>(500,"你并被录入,请前往录入");
        }
        return new ResResult<>(200,"提交成功");
    }

}
